package cn.train.controler;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.train.model.Query;
import cn.train.model.ZuoType;
import cn.train.model.Zuo_notes;
import cn.train.service.QueryTrainService;

@WebServlet("/IndexMainServlet")
public class IndexMainServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
   @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	   HttpSession sesssion = req.getSession();
	   Date date12=new Date();
	SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sd= new SimpleDateFormat("HH-mm-ss");
    Date nyr,sfm;
	try {
		nyr = sdf.parse(sdf.format(date12));
		sfm =sd.parse(sd.format(date12));
		//System.out.println("年月日"+nyr.getTime()+"时分秒"+sfm.getTime());
		sesssion.setAttribute("nyr",nyr);
		sesssion.setAttribute("sfm",sfm);
	} catch (ParseException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
   
    
    
	   
	String city1=req.getParameter("city1");
	String city2=req.getParameter("city2");
	String time1=req.getParameter("time1");
	String time2=req.getParameter("time2");
	String flag=req.getParameter("flag");
	SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
	QueryTrainService querys=new QueryTrainService();
	List<ZuoType>zuotype=querys.QueryPrice();
	PrintWriter out =resp.getWriter();
	
    Date d;
    System.out.println("flag"+time1);
    if(flag!=null)
	try {
		d = s.parse(time1);
		java.sql.Date date = new java.sql.Date(d.getTime());
		if("0".equals(flag)){
			
			List<Query>list=querys.QueryByDanCheng(city1,city2,date);
			
		/*	for(Query q:list){
				System.out.println(q.getGoname());
			}*/
			if(list.size()==0){
				req.setAttribute("flag", 1);
				
				req.getRequestDispatcher("font/index.jsp").forward(req, resp);
				return;
			}else{
			/*	System.out.println(time1);
				System.out.println(time1.substring(time1.length()-2));*/
				BigDecimal jibenjia=null;
				
				for(Query q:list){
					jibenjia=q.getJibenjia();
					break;
				}
				Integer f=  querys.addjilu(city1, city2, jibenjia);
				System.out.println("新增记录："+f);
				
				time2=s.format(new Date(d.getTime() + (long)2 * 24 * 60 * 60 * 1000));
				Date ti1,ti2;
				try {
					ti1 = sdf.parse(time1);
					ti2 = sdf.parse(time2);
					//System.out.println("年月日"+nyr.getTime()+"时分秒"+sfm.getTime());
					sesssion.setAttribute("ti1",ti1);
					sesssion.setAttribute("ti2",ti2);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				sesssion.setAttribute("count", list.size());
				sesssion.setAttribute("flag", "单程");
				sesssion.setAttribute("city1", city1);
				sesssion.setAttribute("city2", city2);
				sesssion.setAttribute("time1", time1);
				sesssion.setAttribute("time2", time2);
				sesssion.setAttribute("lis", list);
				sesssion.setAttribute("zuotype", zuotype);
				req.getRequestDispatcher("font/show.jsp").forward(req, resp);
				return;
			}
			
		}else if("1".equals(flag)){
			d = s.parse(time2);
			java.sql.Date date1 = new java.sql.Date(d.getTime());
			List<Query>list=querys.QueryByDanCheng(city1,city2,date);
			List<Query>list1=querys.QueryByDanCheng(city2,city1,date1);
		/*	for(Query q:list1){
				System.out.println(q.getGoname());
			}*/
			if(list.size()==0){
				req.setAttribute("flag", 1);
				req.getRequestDispatcher("font/index.jsp").forward(req, resp);
				return;
			}else{
				
                BigDecimal jibenjia=null;
				
				for(Query q:list){
					jibenjia=q.getJibenjia();
					break;
				}
				Integer f=  querys.addjilu(city1, city2, jibenjia);
				System.out.println("新增记录："+f);
				
				System.out.println("改动估计"+list1.size());
				Date ti1,ti2;
				try {
					ti1 = sdf.parse(time1);
					ti2 = sdf.parse(time2);
					//System.out.println("年月日"+nyr.getTime()+"时分秒"+sfm.getTime());
					sesssion.setAttribute("ti1",ti1);
					sesssion.setAttribute("ti2",ti2);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				sesssion.setAttribute("count", list.size());
				sesssion.setAttribute("count1", list1.size());
				sesssion.setAttribute("flag", "往返");
				sesssion.setAttribute("city1", city1);
				sesssion.setAttribute("city2", city2);
				sesssion.setAttribute("time1", time1);
				sesssion.setAttribute("time2", time2);
				sesssion.setAttribute("lis", list);
				sesssion.setAttribute("lis1", list1);
				sesssion.setAttribute("zuotype", zuotype);
				req.getRequestDispatcher("font/show.jsp").forward(req, resp);
				return;
			}
			
		}
		
	   
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  /* System.out.println("1");
	   
		String st=req.getParameter("ipts"); 
		out.write("<h1>和富士康的经费和风景和水果放胡椒粉和咖啡挥洒放回口袋就撒复活赛偶发哈斯会对谁哦度港囧的价格咯的附属机构咯的方式国际大牌即佛都是高级哦都市国际电联分配给囧的废品收购基地拍摄根据地佛牌机构的世界观咯的是国家的佛手柑机票都试过觉得送苹果就看大佛【哦骨骼肌哦哦四大过的第戎公司的价格咯数据是怕；1</h1>");
		  System.out.println(st);*/
	
	}
    

	
   
   @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   doPost(req, resp);
   }
}
